//
//  UIBarButtonItem+Extension.swift
//  MicroBlog
//
//  Created by Apple on 16/9/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

//给UIBarButtonItem扩展一个便利构造函数
extension UIBarButtonItem {
    // 设置传入的参数
    convenience init(title: String, imageName: String? = nil, target: Any? = nil, action: Selector? = nil) {
        //使用self调用其他构造函数
        self.init()
        
        let button = UIButton()
        //设置图片
        if imageName != nil{
            button.setImage(UIImage(named: imageName!), for: .normal)
        }
        //添加点击事件
        if target != nil && action != nil {
            button.addTarget(target!, action: action!, for: .touchUpInside)
        }
        button.setTitle(title, for: .normal)
        //设置文字颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.sizeToFit()
        
        customView = button
    }
}
